package com.zcccccy.rocketmq.producer.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.UUID;
import cn.hutool.core.util.StrUtil;
import com.zcccccy.rocketmq.producer.service.MessageProducer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @projectName: zcccccy
 * @packageName: com.zcccccy.extend.rocketmq.producer.controller
 * @author: zcy
 * @createTime: 2024/1/29
 * @productName: For Mac IntelliJ IDEA
 */
@RestController
@RequestMapping("/message/producer")
public class MessageProducerController {

    private static final Logger log = LoggerFactory.getLogger(MessageProducerController.class);

    @Autowired
    private MessageProducer messageProducer;

    @GetMapping("/send")
    public String send(String message) {

        if (StrUtil.isBlank(message)) {
            message = "消息" + UUID.randomUUID();
        }

        log.info("发送时间：{}", DateUtil.date());

        return messageProducer.sendMessage("system-message", "tag", UUID.randomUUID().toString(), message);
    }

    @GetMapping("/sendDelay")
    public String sendDelay(String message) {

        if (StrUtil.isBlank(message)) {
            message = "延时消息" + UUID.randomUUID();
        }


        log.info("发送时间：{}", DateUtil.date());

        // 延迟俩分钟
        Long delay = 2L * 60 * 1000;

        return messageProducer.sendDelayMessage("delay-message", "tag", UUID.randomUUID().toString(), message, delay);
    }

    @GetMapping("/sendTimed")
    public String sendTimedMessage(String message) {

        if (StrUtil.isBlank(message)) {
            message = "定时消息" + UUID.randomUUID();
        }

        // 延迟后的时间戳。
        Long deliverTimeStamp = System.currentTimeMillis() + 2L * 60 * 1000;

        log.info("发送时间：{}", DateUtil.date());

        return messageProducer.sendTimedMessage("delay-message", "tag", UUID.randomUUID().toString(), message, deliverTimeStamp);
    }
}
